extends VBoxContainer

#@onready var _addBtn:Button = $"HBoxContainer2/AddButton"
@onready var _itemRoot = $"ScrollContainer/SettingRoot"
@onready var _item = $"ScrollContainer/SettingRoot/ProjectSetting"

var _hasInit = false

# Called when the node enters the scene tree for the first time.
func _ready():
	_hasInit = true
	_refresh()


func _refresh():
	var count = Global.config_datas.size()
	while _itemRoot.get_child_count() < count:
		var go = _item.duplicate()
		_itemRoot.add_child(go)
	
	for i in range(_itemRoot.get_child_count()):
		var child = _itemRoot.get_child(i)
		child.visible = i < count
		
	var datas = Global.config_datas
	for i in range(datas.size()):
		var key = datas.keys()[i]
		var item = _itemRoot.get_child(i)
		item.refresh(key)


func _on_visibility_changed():
	if visible and _hasInit:
		_refresh()


func _on_add_button_button_down() -> void:
	var time = Time.get_unix_time_from_system()
	var defaultProjectName = str(time)
	Global.add_project(defaultProjectName)
	
	var go = _item.duplicate()
	_itemRoot.add_child(go)
	go.refresh(defaultProjectName)
